package com.fc.v2.controller.gen;

import com.fc.v2.common.base.BaseController;
import com.fc.v2.common.domain.AjaxResult;
import com.fc.v2.common.domain.ResultTable;
import com.fc.v2.model.ScoreRank;
import com.fc.v2.model.auto.FinishProject;
import com.fc.v2.model.auto.TsysUser;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.model.auto.ScoreProject;
import com.fc.v2.model.wapper.ScoreProjectWapper;
import com.fc.v2.service.FinishProjectService;
import com.fc.v2.service.ScoreProjectService;
import com.fc.v2.service.SysUserService;
import com.fc.v2.util.BeanUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 专家鉴定评分Controller
 * @ClassName: ScoreProjectController
 * @author Archie
 * @date 2021-11-15 10:58:33
 */
@Api(value = "专家鉴定评分")
@Controller
@RequestMapping("/FinishScoreController")
public class FinishScoreController extends BaseController{
	
	private String prefix = "gen/auditFinishExpert";
	
	@Autowired
	private ScoreProjectService scoreProjectService;

	//系统用户
	@Autowired
	public SysUserService sysUserService;

	@Autowired
	private FinishProjectService finishProjectService;


	/**
	 * 专家鉴定评分页面展示
	 * @param model
	 * @return String
	 * @author Archie
	 */
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/view")
	@RequiresPermissions("gen:applyProject:view")
    public String view(ModelMap model)
    {
        return prefix + "/list";
    }
	
	/**
	 * list集合
	 * @param tablepar
	 * @param searchText
	 * @return
	 */
	//@Log(title = "专家鉴定评分", action = "111")
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/list/{proId}")
	@RequiresPermissions("gen:applyProject:list")
	@ResponseBody
	public ResultTable list(Tablepar tablepar, ScoreProject scoreProject, @PathVariable("proId") String proId, Model model){
		//page为该项目相关的数据（项目id相等)
		PageInfo<ScoreProject> page=scoreProjectService.list(tablepar,scoreProject,proId);

		//将ScoreProject封装到ScoreProjectWapper中
		List<ScoreProject> pageList = page.getList();
		List<ScoreProjectWapper> res = new ArrayList<ScoreProjectWapper>();
		//鉴定平均分
		Float resScore = 0F;
		Float sumScore = 0F;
		//优秀的专家评定数
		int yxNum = 0;
		//良好秀的专家评定数
		int lhNum = 0;
		//合格的专家评定数
		int hgNum = 0;
		//优秀
		int yxFlag = 0;
		//良好
		int lhFlag = 0;
		//合格
		int hgFlag = 0;

		for (ScoreProject sp:pageList) {
			ScoreProjectWapper wapper = new ScoreProjectWapper();
			BeanUtils.copyBeanProp(wapper,sp);
			//通过专家的id查询专家名称
			String expertName;
			TsysUser user = sysUserService.selectByPrimaryKey(sp.getExpertId());
			expertName = user.getNickname();
			wapper.setExpertName(expertName);
			res.add(wapper);
			//鉴定总分
			sumScore += sp.getScoreTotal();
			if(sp.getScoreTotal() >= 8.5){
				yxNum++;
			}else if(sp.getScoreTotal()<8.5 && sp.getScoreTotal()>=7.5){
				lhNum++;
			}else if(sp.getScoreTotal()<7.5 && sp.getScoreTotal()>=6.5){
				hgNum++;
			}
		}
		FinishProject finishProject = finishProjectService.selectByPrimaryKey(proId);
		resScore = sumScore/pageList.size();
		//计算鉴定平均分
		if(resScore.isNaN() || resScore == 0F){
			finishProject.setProAvgScore("");
		}else {
			finishProject.setProAvgScore(resScore.toString());
		}

		//计算专家推荐等级
		if(resScore != 0){
			if(yxNum>=2 && resScore>=8.5){
				yxFlag++;
				finishProject.setProRecRank(ScoreRank.EXCELLEN.code);
			}else if(resScore>=7.5 && (yxNum+lhNum)>=2 && yxFlag==0){
				lhFlag++;
				finishProject.setProRecRank(ScoreRank.GOOD.code);
			}else if(resScore>=6.5 && (yxNum+lhNum+hgNum)>=2 && lhFlag==0 && yxFlag==0){
				hgFlag++;
				finishProject.setProRecRank(ScoreRank.QUALIFIED.code);
			}else {
				finishProject.setProRecRank(ScoreRank.NO_QUALIFIED.code);
			}
		}

		finishProjectService.updateByPrimaryKeySelective(finishProject);
		return pageTable(res,page.getTotal());
	}
	
	/**
     * 新增跳转
     */
	@ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }
	
    /**
     * 新增保存
     * @param 
     * @return
     */
	//@Log(title = "专家鉴定评分新增", action = "111")
	@ApiOperation(value = "新增", notes = "新增")
	@PostMapping("/add/{projectId}")
	@RequiresPermissions("gen:applyProject:add")
	@ResponseBody
	public AjaxResult add(ScoreProject scoreProject,@PathVariable("projectId") String projectId){
		int b=scoreProjectService.insertSelectiveFinish(scoreProject,projectId);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 专家鉴定评分删除
	 * @param ids
	 * @return
	 */
	//@Log(title = "专家鉴定评分删除", action = "111")
	@ApiOperation(value = "删除", notes = "删除")
	@DeleteMapping("/remove")
	@RequiresPermissions("gen:applyProject:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=scoreProjectService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map)
    {
        map.put("ScoreProject", scoreProjectService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

	/**
	 * 专家结项鉴定评分跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/finishScore/{id}")
	public String auditScore(@PathVariable("id") String id, ModelMap map)
	{
		map.put("FinishProject", finishProjectService.selectByPrimaryKey(id));

		return prefix + "/finishScore";
	}
	
	/**
     * 修改保存
     */
    //@Log(title = "专家鉴定评分修改", action = "111")
	@ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:applyProject:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ScoreProject scoreProject)
    {
        return toAjax(scoreProjectService.updateByPrimaryKeySelective(scoreProject));
    }
    
    
    /**
	 * 修改状态
	 * @param record
	 * @return
	 */
    @PutMapping("/updateVisible")
	@ResponseBody
    public AjaxResult updateVisible(@RequestBody ScoreProject scoreProject){
		int i=scoreProjectService.updateVisible(scoreProject);
		return toAjax(i);
	}

    
    

	
}
